package com.xiyue.server.controller;

import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.session.SaSessionCustomUtil;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.sport.common.utils.PasswordUtils;
import com.sport.common.web.controller.BaseController;
import com.sport.common.web.model.Result;
import com.sport.model.entity.SysUser;
import com.xiyue.server.annotation.RestControllerMapping;
import com.xiyue.server.service.SysUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Tag(name = "当前登录用户")
@RestControllerMapping("currentUser")
public class CurrentUserController  extends BaseController {
    @Resource
    private SysUserService sysUserService;

    @GetMapping("info")
    @Operation(summary = "当前登录信息")
    public Result<SysUser> info() {
        String loginId = StpUtil.getLoginIdAsString();
        SysUser user = sysUserService.getById(loginId);
        user.setPassword(PasswordUtils.decrypt(user.getSalt(), user.getPassword()));
        return Result.success(user);
    }

    @GetMapping("menuList")
    @Operation(summary = "用户菜单列表")
    public Result<List<JSONObject>> menuList() {
        SaSession saSession = StpUtil.getSessionByLoginId(StpUtil.getLoginIdAsString());
        List<JSONObject> menus = saSession.get("menu",
                () -> sysUserService.listMenuByUserId());
        return Result.success(menus);
    }

    @GetMapping("cleanCache")
    @Operation(summary = "清除缓存")
    public Result<Boolean> clearCache(){
        SaSession session = StpUtil.getSession();
        session.clear();
        return Result.success(true);
    }

    @GetMapping("getPerms")
    @Operation(summary = "获取当前登录用户的角色列表和权限列表")
    public Result<Map<String,Object>> getPerms(){
        List<String> perms = new ArrayList<>();
        Map<String,Object> map = new HashMap<>(2);
        List<String> roles = StpUtil.getSession().get("Role_List", sysUserService.listRoleByUserId(StpUtil.getLoginIdAsString()));
        for (String role : roles) {
            SaSession saSession = SaSessionCustomUtil.getSessionById("role-" + role);
            List<String> permissionList = saSession.get("Permission_List", () -> sysUserService.listPermissionByRoleCode(role));
            perms.addAll(permissionList);
        }

        map.put("roles",roles);
        map.put("perms",perms);
        return Result.success(map);
    }

    @PostMapping("updateUser")
    public Result<SysUser> updateUser(@RequestBody SysUser user){
        user.setId(StpUtil.getLoginIdAsString());
        if(StrUtil.isNotBlank(user.getPassword())){
            String slat = RandomUtil.randomString(16);
            user.setSalt(slat);
            String password = user.getPassword();
            String enPassword = PasswordUtils.encrypt(user.getSalt(), password);
            user.setPassword(enPassword);
        }
        return toResult(sysUserService.updateById(user));
    }
}
